Metaheuristics

Genetic Algorithms

References:

Load Libraries

library(ggplot2)
## Warning: package 'ggplot2' was built under R version 3.4.4
library(GA)
## Warning: package 'GA' was built under R version 3.4.4
## Loading required package: foreach
## Loading required package: iterators
## Warning: package 'iterators' was built under R version 3.4.4
## Package 'GA' version 3.1.1
## Type 'citation("GA")' for citing this R package in publications.

Define Function

# The following function requires two inputs
# Limits are (-3,-3) to (3, 3)
 fun = function(x,y) { (sin(10*x)*cos(10*y)+2)/sqrt(x^4+y^4+1) } 
 obj = function(z) { fun(z[1],z[2])}
 
# Plot the function
x=seq(-3,3,length=50) # tick marks on x axis
y=seq(-3,3,length=50) # tick marks on y axis; defines grid for... 
z=outer(x,y,fun) # matrix for plotting -- z vals / height of surface
 persp3D(x,y, z, phi = 0, theta = 45,
  xlab = "X", ylab = "Y",
  main = "Surface elevation data",
  color.palette = bl2gr.colors,
  ticktype = "detailed"
)

filled.contour(x, y, z, color.palette = bl2gr.colors)

Solve using GA

https://cran.r-project.org/web/packages/GA/vignettes/GA.html

library(GA)
lower = c(-3,-3)
upper = c(3,3)
# Visualize the solution
monitor <- function(obj) 
{ 
  contour(x, y, z, drawlabels = FALSE, col = grey(0.5))
  title(paste("iteration =", obj@iter), font.main = 1)
  points(obj@population, pch = 20, col = 2)
  Sys.sleep(0.2)
}

GA1 <- ga(type = "real-valued",fitness=obj,lower=lower,upper=upper,
          popSize = 50, monitor = monitor)

summary(GA1)
## ── Genetic Algorithm ─────────────────── 
## 
## GA settings: 
## Type                  =  real-valued 
## Population size       =  50 
## Number of generations =  100 
## Elitism               =  2 
## Crossover probability =  0.8 
## Mutation probability  =  0.1 
## Search domain = 
##       x1 x2
## lower -3 -3
## upper  3  3
## 
## GA results: 
## Iterations             = 100 
## Fitness function value = 2.984763 
## Solution = 
##              x1        x2
## [1,] -0.1569031 0.3124193
plot(GA1)

filled.contour(x, y, z, color.palette = bl2gr.colors, 
      plot.axes = { axis(1);
                    axis(2); points(GA1@solution[,1],GA1@solution[,2],
                                    cex=2,col="red",lwd=2)}
)

Define another function

# Define another function
 nfun = function(x) {
   (12*x^5-975*x^4+28000*x^3-345000*x^2+1800000*x)
 }
ll = c(0)
ul = c(31)

# Plot the function
# Note, plotting as a maximization function
p <- ggplot(data=data.frame(x=0),aes(x=x))
p + stat_function(fun=nfun) + xlim(0,31)

 nfun1 = function(x) {
   (12*x^5-975*x^4+28000*x^3-345000*x^2+1800000*x)
 }
GA2 <- ga(type = "real-valued",fitnes=nfun1, lower=0,upper=31)
summary(GA2)
## ── Genetic Algorithm ─────────────────── 
## 
## GA settings: 
## Type                  =  real-valued 
## Population size       =  50 
## Number of generations =  100 
## Elitism               =  2 
## Crossover probability =  0.8 
## Mutation probability  =  0.1 
## Search domain = 
##       x1
## lower  0
## upper 31
## 
## GA results: 
## Iterations             = 100 
## Fitness function value = 4400000 
## Solution = 
##            x1
## [1,] 20.00019
plot(GA2)

curve(nfun1, from = 0, to = 31, n = 1000)
points(GA2@solution, GA2@fitnessValue, col = 2, pch = 19)